﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BobcatUtility
{
    /// <summary>
    /// This class lets you log messages. It is a singleton, so it can be used globally.
    /// It is observable via events to make it easy to show the logged data.
    /// </summary>
    public class Logger
    {
        #region Public declarations

        public enum Level
        {
            DEBUG,
            NOTICE,
            WARN,
            ERROR,
            FATAL
        }

        #endregion

        #region Events

        /// <summary>
        /// Data passed with events raised by the Logger class.
        /// </summary>
        public class Args : EventArgs
        {
            public Level Level { get; set; }
            public string Message { get; set; }
        }

        /// <summary>
        /// Event raised when new messages are logged.
        /// </summary>
        public event EventHandler<Args> MessageLoggedEvent;

        #endregion

        #region Public methods

        /// <summary>
        /// Singleton getInstance() method.
        /// </summary>
        public static Logger getInstance()
        {
            if (null == m_Instance)
            {
                m_Instance = new Logger();
            }
            return m_Instance;
        }

        /// <summary>
        /// Logs a message.
        /// </summary>
        public void log(string strMessage, Level eLevel)
        {
            // We raise an event to any of our observers...
            Args args = new Args();
            args.Message = strMessage;
            args.Level = eLevel;
            Utility.CrossThreadInvoke(MessageLoggedEvent, this, args);
        }

        #endregion

        #region Private functions

        /// <summary>
        /// Constructor is private (for the singleton).
        /// </summary>
        private Logger()
        {
        }

        #endregion

        #region Private data

        private static Logger m_Instance = null;

        #endregion
    }
}
